<script setup lang="ts">
import { useProfile } from '@/stores'
import { useToast } from '@/utils'
import { useQRCode } from '@vueuse/integrations/useQRCode'
import { to } from 'await-to-js'
import { ref, shallowRef } from 'vue'

const popup = ref()
const codeUrl = ref()
const profile = useProfile()
const qrcodeRef = ref()
async function open() {
  const text = shallowRef(generateQRCodeUrl())
  codeUrl.value = useQRCode(text)
  popup.value.open()
}
// 生成二维码 URL 的函数
function generateQRCodeUrl(): string {
  // const baseUrl = 'https://partner.xmquanxin.com/#/add/index'
  const baseUrl = `${window.location.origin}/#/add/index`
  const queryParams = new URLSearchParams({ userId: profile.userInfo.id })
  return `${baseUrl}?${queryParams}`
}
async function saveQRCode() {
  // qrcodeRef.value.save(() => {
  //   success:(res) => {
  //     console.log(res)
  //   }
  // })
}

defineExpose({
  open,
})
</script>

<template>
  <view>
    <uv-popup ref="popup" closeable>
      <view>
        <view class="mt-20px px-2 lh-40px">二维码</view>
        <view class="h-400px w-400px flex justify-center">
          <!-- <uv-qrcode
            ref="qrcodeRef"
            size="200px"
            :value="codeUrl"
            @longpress="saveQRCode"
          >
          </uv-qrcode> -->
          <img :src="codeUrl.value">
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<style>
</style>
